﻿/* dk2Repository - Copyright (C) 2011 Daniel Kuczewski http://www.dk2.de/

 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

using System;
using System.Data;
using dk2Database.Commands;
using dk2Database.Interfaces;

namespace dk2Database
{

    /// <summary>
    /// Provides execute and query functions for specific dbconnection
    /// </summary>
    public abstract class Database : IDisposable, IConnectionFactory
    {
        protected Database()
        {
            IsDisposed = false;
        }

        protected abstract IDbConnection CreateDbConnection();

        public bool IsDisposed { get; private set; }

        public IConnection CreateConnection()
        {
            try
            {
                var dbcon = CreateDbConnection();
                dbcon.Open();

                return new SingleUseConnection(dbcon);
            }
            catch (Exception ex)
            {
                throw new ConnectionException(null, ex.Message);
            }
        }

        public DataTable Query(string sql, params object[] parameters)
        {
            var cmd = new QueryTableCommand(sql, parameters);
            cmd.ConnectionFactory = this;
            cmd.Execute();
            return cmd.Result;
        }

        public DataTable Query(QueryTableCommand cmd)
        {
            cmd.ConnectionFactory = this;
            cmd.Execute();
            return cmd.Result;
        }

        public T Query<T>(Func<IDataReader, T> handleReader, string sql, params object[] parameters)
        {
            var cmd = new LambdaQueryEntityCommand<T>(handleReader, sql, parameters);
            cmd.ConnectionFactory = this;
            cmd.Execute();
            return cmd.Result;
        }

        public T Query<T>(QueryEntityCommandBase<T> cmd)
        {
            cmd.ConnectionFactory = this;
            cmd.Execute();
            return cmd.Result;
        }

        public int Execute(string sql, params object[] parameters)
        {
            var cmd = new ExecuteCommand(sql, parameters);
            cmd.ConnectionFactory = this;
            cmd.Execute();
            return (int)cmd.Result;
        }

        public int Execute(ExecuteCommand cmd)
        {
            cmd.ConnectionFactory = this;
            cmd.Execute();
            return (int)cmd.Result;
        }

        public object ExecuteScalar(string sql, params object[] parameters)
        {
            var cmd = new ExecuteScalarCommand(sql, parameters);
            cmd.ConnectionFactory = this;
            cmd.Execute();
            return cmd.Result;
        }

        public object ExecuteScalar(ExecuteScalarCommand cmd)
        {
            cmd.ConnectionFactory = this;
            cmd.Execute();
            return cmd.Result;
        }

        public virtual void Dispose()
        {
            IsDisposed = true;
        }


    }
}
